

var role = -1; // 选择的角色

function checkPhone(phone) {
    if(phone.search(/^1[3458]{1}[0-9]{9}$/) == -1)
        return false;
    else
        return true;
}

function checkUsername(value) {
    if(value.search(/^[a-zA-Z_][a-zA-Z0-9_]{3,15}$/) == -1)
        return false;
    else
        return true;
}

function checkPass(value) {
    if(value.search(/^.{6,22}$/) == -1)
        return false;
    else
        return true;
}

function checkPerson(value) {
    if(value.search(/^.{2,22}$/) == -1)
        return false;
    else
        return true;
}

function checkEmail(value) {
    if(value.search(/^[\w\.+-]+@[\w\.+-]+$/) == -1)
        return false;
    else
        return true;
}

function checkCode(value) {
    if(value.search(/^\d{4}$/) == -1)
        return false;
    else
        return true;
}

var register = {
    chooseRole: function() {
        $('.role li').on('click', function() {
            var $this = $(this);
            var index = $this.attr('data-id');
            $this.addClass('is-active').siblings().removeClass('is-active');
            role = index;
        })
    },
    check: function() {
        $('[data-role="username"]').on('blur', function() {
            var value = $(this).val();
            if(value == '' || !checkUsername(value)) {
                $(this).next('.tips').html('字母或下划线开头，包含4-16位字母或数字');
            } else {
                $(this).next('.tips').html('');
            }
        })

        $('[data-role="password"]').on('blur', function() {
            var value = $(this).val();
            if(value == '' || !checkPass(value)) {
                $(this).next('.tips').html('长度需为6-22个字符');
            } else {
                $(this).next('.tips').html('');
            }
        })

        $('[data-role="rePass"]').on('blur', function() {
            var value = $(this).val();
            if(value != $('[data-role="password"]').val()) {
                $(this).next('.tips').html('两次输入密码不一致');
            }
            else {
                $(this).next('.tips').html('');
            }
        })

        $('[data-role="person"]').on('blur', function() {
            var value = $(this).val();
            if(value == '' || !checkPerson(value)) {
                $(this).next('.tips').html('长度需为2-22个字符');
            } else {
                $(this).next('.tips').html('');
            }
        })

        $('[data-role="imgcode"]').on('blur', function() {
            var value = $(this).val();
            if(value == '') {
                $(this).next().next('.tips').html('请输入验证码');
            } else {
                $(this).next().next('.tips').html('');
            }
        })

        $('[data-role="phone"]').on('blur', function() {
            var value = $(this).val();
            if(value == '' || !checkPhone(value)) {
                $(this).next('.tips').html('请输入正确的手机号');
            } else {
                $(this).next('.tips').html('');
            }
        })

        $('[data-role="code"]').on('blur', function() {
            var value = $(this).val();
            if(value == '' || !checkCode(value)) {
                $(this).next().next('.tips').html('请输入4位数的验证码');
            } else {
                $(this).next().next('.tips').html('');
            }
        })
    },
    submit: function() {

        $('[data-role="submit"]').on('click', function() {
            if(role == -1) {
                iosOverlay({
                    text: '请先选择注册的角色',
                    duration: 1000
                });
            } else {
                var username = $('[data-role="username"]').val();
                if(username == '' || !checkUsername(username)) {
                    $('[data-role="username"]').next('.tips').html('字母或下划线开头，包含4-16位字母或数字');
                } else {
                    $('[data-role="username"]').next('.tips').html('');
                }

                var password = $('[data-role="password"]').val();
                if(password == '' || !checkPass(password)) {
                    $('[data-role="password"]').next('.tips').html('长度需为6-22个字符');
                } else {
                    $('[data-role="password"]').next('.tips').html('');
                }

                var rePass = $('[data-role="rePass"]').val();
                if(rePass != password) {
                    $('[data-role="rePass"]').next('.tips').html('两次输入密码不一致');
                }
                else {
                    $('[data-role="rePass"]').next('.tips').html('');
                }

                var person = $('[data-role="person"]').val();
                if(person == '' || !checkPerson(person)) {
                    $('[data-role="person"]').next('.tips').html('长度需为2-22个字符');
                } else {
                    $('[data-role="person"]').next('.tips').html('');
                }

                var imgcode = $('[data-role="imgcode"]').val();
                if(imgcode == '') {
                    $('[data-role="imgcode"]').next().next('.tips').html('请输入验证码');
                } else {
                    $('[data-role="imgcode"]').next().next('.tips').html('');
                }

                var phone = $('[data-role="phone"]').val();
                if(phone == '' || !checkPhone(phone)) {
                    $('[data-role="phone"]').next('.tips').html('请输入正确的手机号');
                } else {
                    $('[data-role="phone"]').next('.tips').html('');
                }

                var qq = $('[data-role="qq"]').val();

                var code = $('[data-role="code"]').val();
                if(code == '' || !checkCode(code)) {
                    $('[data-role="code"]').next().next('.tips').html('请输入4位数的验证码');
                } else {
                    $('[data-role="code"]').next().next('.tips').html('');
                }

                // if(role == 0) {
                //     var company = $('[data-role="company"]').val();
                // }

                // 同意协议
                if(!$('[data-role="xieyi"]').prop('checked') && checkUsername(username) && checkPass(password) && rePass == password && checkPerson(person) && checkPhone(phone) && checkCode(code) && imgcode != '') {
                    iosOverlay({
                        text: '请先同意协议',
                        duration: 1000
                    });
                }

                // 提交
                if($('[data-role="xieyi"]').prop('checked') && checkUsername(username) && checkPass(password) && rePass == password && checkPerson(person) && checkPhone(phone) && checkCode(code) && imgcode != '') {
                    var obj = {
                        user: username,
                        pass: password,
                        contact: person,
                        mobile: phone,
                        qq: qq,
                        code: code,
                        type: role
                    }
                    $.ajax({
                        url: window.domain + window.requestUrl.registerUrl + role,
                        type: 'post',
                        data: obj,
                        xhrFields: {
                            withCredentials: true
                        },
                        crossDomain: true,
                        success: function(res) {
                            if(typeof(res.not_login) != 'undefined') {
                                window.setCookie('go_back_link', window.location.href);
                                window.location.href = './login.html';
                                return;
                            }
                            if(res.status) {
                                iosOverlay({
                                    text: '注册成功',
                                    duration: 1000,
                                    onbeforehide: function() {
                                        window.location.href = './login.html';
                                    }
                                });
                            } else {
                                iosOverlay({
                                    text: res.msg,
                                    duration: 1000
                                });
                                $('[data-role="imgcodemain"]').attr('src', window.domain + window.requestUrl.imgCodeUrl);
                            }
                        },
                        error: function(e) {
                        }
                    })
                }
            }
        })
    },
    sendCode: function() {
        var countdown=60;
        function settime(obj) {
            if (countdown == 0) {
                obj.removeAttr("disabled");
                obj.text("重新获取验证码");
                countdown = 60;
                return;
            } else {
                obj.attr("disabled", "disabled");
                obj.text(countdown + "秒后重新发送");
                countdown--;
            }
            setTimeout(function() {
                settime(obj)
            }, 1000)
        }
        $('.sendcode').on('click', function() {
            if(role == -1) {
                iosOverlay({
                    text: '请先选择注册的角色',
                    duration: 1000
                });
                return;
            }
            if(checkPhone($('[data-role="phone"]').val()) && $('[data-role="imgcode"]').val() != '') {
                var $this = $(this);
                if(!$this.attr('disabled')) {
                    $.ajax({
                        url: window.domain + window.requestUrl.registerCodeUrl,
                        type: 'get',
                        data: {
                            mobile: $('[data-role="phone"]').val(),
                            imgCode: $('[data-role="imgcode"]').val(),
                            type: (Number(role) + 1)
                        },
                        xhrFields: {
                            withCredentials: true
                        },
                        crossDomain: true,
                        success: function(res) {
                            if(typeof(res.not_login) != 'undefined') {
                                window.setCookie('go_back_link', window.location.href);
                                window.location.href = './login.html';
                                return;
                            }
                            register.imgCode();
                            if(res.status) {
                                iosOverlay({
                                    text: '验证码发送成功，请注意查收',
                                    duration: 1000
                                });
                                settime($this);
                            } else {
                                iosOverlay({
                                    text: res.msg,
                                    duration: 1000
                                });
                                $('[data-role="imgcodemain"]').attr('src', window.domain + window.requestUrl.imgCodeUrl);
                            }
                        },
                        error: function(e) {
                        }
                    })
                }
            } else {
                if(!checkPhone($('[data-role="phone"]').val())) {
                    iosOverlay({
                        text: '请输入正确的手机号码',
                        duration: 1000
                    });
                } else {
                    if($('[data-role="imgcode"]').val() == '') {
                        iosOverlay({
                            text: '请输入验证码',
                            duration: 1000
                        });
                    }
                }


            }
        })
    },
    imgCode: function() {
        $('[data-role="imgcodemain"]').attr('src', window.domain + window.requestUrl.imgCodeUrl);
        $('[data-role="imgcodemain"]').on('click', function() {
            $(this).attr('src', window.domain + window.requestUrl.imgCodeUrl + '?' + Math.random());
        })
    },
    init: function() {
        this.submit();
        this.chooseRole();
        this.check();
        this.sendCode();
        this.imgCode();
    }
}

register.init();
